/**
 * 
 */
package edu.pw.treegrid.server.filter;

import java.util.HashSet;
import java.util.Set;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.pw.treegrid.server.reportmodel.Report;

public abstract class ComplexFilter implements Filter {

	private Set<Filter> filters = new HashSet<Filter>();

	public Set<Filter> getFilters() {
		return filters;
	}

	public void setFilters(Set<Filter> filters) {
		this.filters = filters;
	}

	public void addFilter(Filter filter) {
		this.filters.add(filter);
	}
	
	public Element serializeToXML(Document document) {
		Element element = document.createElement(getXMLElementName());
		for(Filter f: filters) {
			element.appendChild(f.serializeToXML(document));
		}
		return element;
	}
	
	public void deserializeFromXML(Element filterElement,
			Report report) {
		NodeList list = filterElement.getChildNodes();
		for(int i=0;i<list.getLength();++i) {
			Node n = list.item(i);
			if(n instanceof Element) {
				addFilter(FilterDeserializer.newFilter((Element)n, report));
			}
		}
	}
}
